package com.example.choupaiapp.main.activity;

import android.Manifest;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.choupaiapp.main.bean.SendCodeBean;
import com.example.choupaiapp.R;
import com.example.choupaiapp.common.constant.Contacts;
import com.example.choupaiapp.common.finalokgo.callback.Callback;
import com.example.choupaiapp.common.finalokgo.request.RequestType;
import com.example.choupaiapp.common.ui.activity.CommonActivity;
import com.example.choupaiapp.common.util.ActivityManager;
import com.example.choupaiapp.common.util.DialogUtil;
import com.example.choupaiapp.common.util.SharedPreferencesHelp;
import com.example.choupaiapp.common.util.Utils;
import com.example.choupaiapp.main.bean.TokenBean;
import com.example.choupaiapp.main.bean.UserInfoBean;
import com.example.choupaiapp.main.callback.RedBookPresenter;
import com.example.choupaiapp.main.constant.Url;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.lzy.okgo.model.Response;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.Configuration;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UploadManager;
import com.ypx.imagepicker.ImagePicker;
import com.ypx.imagepicker.bean.ImageItem;
import com.ypx.imagepicker.bean.MimeType;
import com.ypx.imagepicker.bean.PickerError;
import com.ypx.imagepicker.data.OnImagePickCompleteListener;
import com.ypx.imagepicker.data.OnImagePickCompleteListener2;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Random;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by ASUS on 2019/10/29.
 */

public class ChangePersonalInfoActivity extends CommonActivity {

    @BindView(R.id.photo)
    CircleImageView photo;
    @BindView(R.id.nikename)
    EditText nikename;
    @BindView(R.id.man_select)
    ImageView man_select;
    @BindView(R.id.woman_select)
    ImageView woman_select;
    @BindView(R.id.next)
    TextView next;
    private int sex = 1;
    private int headflag = 1;
    private UserInfoBean mUserInfoBean;
    private String headPath;
    private Bundle bundle;
    private int flag;
    //    @BindView(R.id.year)
//    WheelView year;
//    @BindView(R.id.month)
//    WheelView month;
//    @BindView(R.id.day)
//    WheelView day;
//    private int startYear = 1900;
//    private int endYear = Calendar.getInstance().get(Calendar.YEAR);
//    private int startMonth = 1;
//    private int endMonth = 12;
//    private int startDay = 1;
//    private int endDay = 31;
//    private int currentYear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_personal_info);
    }

    @Override
    protected void initView() {
        setTitle("个人信息");

        mUserInfoBean = (UserInfoBean) SharedPreferencesHelp.getBean("userInfo");

        bundle = getIntent().getExtras();
        flag = bundle.getInt("flag");

//        year.setCyclic(true);
//        month.setCyclic(true);
//        day.setCyclic(true);
//
//        day.setTextSize(20);
//        month.setTextSize(20);
//        year.setTextSize(20);
//        year.setLabel("年");
//        month.setLabel("月");
//        day.setLabel("日");
//        year.setItemsVisibleCount(5);
//        month.setItemsVisibleCount(5);
//        day.setItemsVisibleCount(5);
//        year.setLineSpacingMultiplier(1);
//        month.setLineSpacingMultiplier(1);
//        day.setLineSpacingMultiplier(1);

//        setSolar(2019, 10, 10);
    }

    @Override
    protected void initData() {
        nikename.setText(mUserInfoBean.getUser().getNickname());
    }

//    @Override
    @OnClick({R.id.add_photo, R.id.man, R.id.woman, R.id.next})
    public void onViewClicked(View view) {
//        super.onViewClicked(view);
        if (Utils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.add_photo:
//                showToast(1900+year.getCurrentItem() + "*" + month.getCurrentItem()+1 + "*" + day.getCurrentItem()+1);
                if (rxPermissions.isGranted(Manifest.permission.CAMERA)) {
                    if (rxPermissions.isGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                        showTakePhoto();
                    } else {
                        rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(aBoolean -> {
                            if (aBoolean) {
                                showTakePhoto();
                            } else {
                                showToast("拒绝权限可能导致功能不能正常使用");
                            }
                        });
                    }

                } else {
                    rxPermissions.request(Manifest.permission.CAMERA).subscribe(aBoolean -> {
                        if (aBoolean) {
                            if (rxPermissions.isGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                                showTakePhoto();
                            } else {
                                rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(aBoolean1 -> {
                                    if (aBoolean1) {
                                        showTakePhoto();
                                    } else {
                                        showToast("拒绝权限可能导致功能不能正常使用");
                                    }
                                });
                            }
                        } else {
                            showToast("拒绝权限可能导致功能不能正常使用");
                        }
                    });
                }
                break;
            case R.id.man:
                sex = 1;
                man_select.setVisibility(View.VISIBLE);
                woman_select.setVisibility(View.INVISIBLE);
                break;
            case R.id.woman:
                sex = 2;
                woman_select.setVisibility(View.VISIBLE);
                man_select.setVisibility(View.INVISIBLE);
                break;
            case R.id.next:
                if (!TextUtils.isEmpty(nikename.getText().toString().trim())) {
                    if (mUserInfoBean != null) {
                        JSONObject jsonObject = new JSONObject();
                        try {
                            jsonObject.put("nickname", nikename.getText().toString().trim());
                            jsonObject.put("sex", sex);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        if (headflag == 1) {
                            showDialog(this, "完善中...");
                            try {
                                jsonObject.put("avatar", mUserInfoBean.getUser().getAvatar());
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            finalOkGo.requestHead(RequestType.POSTJSONHEAD, Url.CHANGE_USERINFO, null, jsonObject.toString(), new Callback<SendCodeBean>() {
                                @Override
                                public void onSuccess(SendCodeBean sendCodeBean) {
                                    if (sendCodeBean.getCode() == Contacts.SUCCESS) {
                                        if (flag == 2) {
                                            EMClient.getInstance().login("HX_" + mUserInfoBean.getUser().getUserId(), "HX_" + mUserInfoBean.getUser().getUserId(), new EMCallBack() {//回调
                                                @Override
                                                public void onSuccess() {
                                                    dismissDialog();
                                                    EMClient.getInstance().groupManager().loadAllGroups();
                                                    EMClient.getInstance().chatManager().loadAllConversations();

                                                    ActivityManager.getActivityManager().popAllActivity();
                                                    startFinishActivity(MainActivity.class);
                                                }

                                                @Override
                                                public void onProgress(int progress, String status) {

                                                }

                                                @Override
                                                public void onError(int code, final String message) {
                                                    runOnUiThread(() -> {
                                                        dismissDialog();
                                                        showImageToas("即时通讯登录失败 私信功能不能使用");
                                                        ActivityManager.getActivityManager().popAllActivity();
                                                        startFinishActivity(MainActivity.class);
                                                    });

                                                }
                                            });
                                        } else {
                                            ActivityManager.getActivityManager().popAllActivity();
                                            startFinishActivity(MainActivity.class);
                                        }
                                    } else {
                                        dismissDialog();
                                        if (sendCodeBean.getCode() == Contacts.TOKEN_EMPTY) {
                                        } else {
                                            showImageToas(sendCodeBean.getMsg());
                                        }
                                    }
                                }

                                @Override
                                public void onError(Response<String> response) {
                                    super.onError(response);
                                    dismissDialog();
                                }
                            });
                        } else if (headflag == 2) {
                            showImageToas("请稍等 正在上传头像");
                        } else {
                            showDialog(this, "完善中...");
                            try {
                                jsonObject.put("avatar", Url.QINIU_YUN + headPath);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            finalOkGo.requestHead(RequestType.POSTJSONHEAD, Url.CHANGE_USERINFO, null, jsonObject.toString(), new Callback<SendCodeBean>() {
                                @Override
                                public void onSuccess(SendCodeBean sendCodeBean) {
                                    if (sendCodeBean.getCode() == Contacts.SUCCESS) {
                                        if (flag == 2) {
                                            EMClient.getInstance().login("HX_" + mUserInfoBean.getUser().getUserId(), "HX_" + mUserInfoBean.getUser().getUserId(), new EMCallBack() {//回调
                                                @Override
                                                public void onSuccess() {
                                                    dismissDialog();
                                                    EMClient.getInstance().groupManager().loadAllGroups();
                                                    EMClient.getInstance().chatManager().loadAllConversations();

                                                    ActivityManager.getActivityManager().popAllActivity();
                                                    startFinishActivity(MainActivity.class);
                                                }

                                                @Override
                                                public void onProgress(int progress, String status) {

                                                }

                                                @Override
                                                public void onError(int code, final String message) {
                                                    runOnUiThread(() -> {
                                                        dismissDialog();
                                                        showImageToas("即时通讯登录失败 私信功能不能使用");
                                                        ActivityManager.getActivityManager().popAllActivity();
                                                        startFinishActivity(MainActivity.class);
                                                    });

                                                }
                                            });
                                        } else {
                                            ActivityManager.getActivityManager().popAllActivity();
                                            startFinishActivity(MainActivity.class);
                                        }
                                    } else {
                                        dismissDialog();
                                        if (sendCodeBean.getCode() == Contacts.TOKEN_EMPTY) {
                                        } else {
                                            showImageToas(sendCodeBean.getMsg());
                                        }
                                    }
                                }

                                @Override
                                public void onError(Response<String> response) {
                                    super.onError(response);
                                    dismissDialog();
                                }
                            });
                        }
                    } else {
                        showImageToas("个人信息获取失败");
                    }

                } else {
                    showImageToas("请填写昵称");
                }
                break;
        }
    }

    private void showTakePhoto() {
        DialogUtil.getInstance().showBottomDialog(this, R.layout.dialog_add_photo);
        LinearLayout dialog_bottom_layout = (LinearLayout) DialogUtil.getInstance().dialog_bottom_layout;

        dialog_bottom_layout.findViewById(R.id.ablum).setOnClickListener(view -> {
            if (Utils.isDoubleClick()) {
                return;
            }
            DialogUtil.getInstance().dialog_bottom.dismiss();
            ImagePicker.withCrop(new RedBookPresenter())//设置presenter
                    .setMaxCount(1)//设置选择数量
                    .showCamera(false)//设置显示拍照
                    .setColumnCount(4)//设置列数
                    .mimeTypes(MimeType.ofImage())//设置需要加载的文件类型
                    .pick(ChangePersonalInfoActivity.this, new OnImagePickCompleteListener2() {
                        @Override
                        public void onPickFailed(PickerError error) {

                        }

                        @Override
                        public void onImagePickComplete(ArrayList<ImageItem> items) {
                            headflag = 2;
                            //图片剪裁回调，主线程
                            //注意：剪裁回调里的ImageItem中getCropUrl()才是剪裁过后的图片地址
                            Glide.with(context).load(items.get(0).getPath()).into(photo);

                            upLoadPic(items.get(0).getPath());

                        }

                    });
        });

        dialog_bottom_layout.findViewById(R.id.take_photo).setOnClickListener(view -> {
            if (Utils.isDoubleClick()) {
                return;
            }
            DialogUtil.getInstance().dialog_bottom.dismiss();
//            CropConfig cropConfig = new CropConfig();
//            cropConfig.setCropRatio(1, 1);//设置剪裁比例
//            cropConfig.setCropRectMargin(0);//设置剪裁框间距，单位px
//            cropConfig.setCropSaveFilePath(ImagePicker.cropPicSaveFilePath);
//            cropConfig.setCircle(false);//是否圆形剪裁
////            cropConfig.setCropStyle(mCbGap.isChecked() ? CropConfig.STYLE_GAP : CropConfig.STYLE_FILL);
////            cropConfig.setCropGapBackgroundColor(mCbGapBackground.isChecked() ? Color.TRANSPARENT : Color.RED);
//            ImagePicker.takePhotoAndCrop(ChangePersonalInfoActivity.this, new WXImgPickerPresenter(), cropConfig, (OnImagePickCompleteListener) items -> {
//                headflag = 2;
//                //剪裁回调，主线程
//                Glide.with(context).load(items.get(0).getPath()).into(photo);
//
//                upLoadPic(items.get(0).getPath());
//            });

            ImagePicker.takePhoto(ChangePersonalInfoActivity.this, "cp_" + System.currentTimeMillis(), true, new OnImagePickCompleteListener() {
                @Override
                public void onImagePickComplete(ArrayList<ImageItem> items) {
                    headflag = 2;
                    //剪裁回调，主线程
                    Glide.with(context).load(items.get(0).getPath()).into(photo);
                    upLoadPic(items.get(0).getPath());

                }
            });

        });

        dialog_bottom_layout.findViewById(R.id.cancel).setOnClickListener(view -> DialogUtil.getInstance().dialog_bottom.dismiss());
    }

    private void upLoadPic(String path) {
        finalOkGo.requestHead(RequestType.POSTHEAD, Url.GET_QINIU_TOKEN, null, Url.GET_QINIU_TOKEN, new Callback<TokenBean>() {
            @Override
            public void onSuccess(TokenBean tokenBean) {
                if (tokenBean.getCode() == Contacts.SUCCESS) {
                    Configuration config = new Configuration.Builder()
                            .build();
                    // 重用uploadManager。一般地，只需要创建一个uploadManager对象
                    UploadManager uploadManager = new UploadManager(config);//配置3个线程数并发上传；不配置默认为3，只针对file.size>4M生效。线程数建议不超过5，上传速度主要取决于上行带宽，带宽很小的情况单线程和多线程没有区别

                    String keyName = System.currentTimeMillis() + "" + (new Random().nextInt(999999) + 100000) + path.substring(path.lastIndexOf("."));

                    uploadManager.put(path, keyName, tokenBean.getToken(), new UpCompletionHandler() {
                        @Override
                        public void complete(String key, ResponseInfo info, JSONObject response) {
                            //res包含hash、key等信息，具体字段取决于上传策略的设置
                            if (info.isOK()) {
                                headflag = 3;
                                headPath = key;
                            } else {
                                //如果失败，这里可以把info信息上报自己的服务器，便于后面分析上传错误原因
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        showImageToas(info.error);
                                    }
                                });

                            }
                        }
                    }, null);
                } else {
                    if (tokenBean.getCode() == Contacts.TOKEN_EMPTY) {
                    } else {
                        showImageToas(tokenBean.getMsg());
                    }
                }
            }
        });
    }



    /*
    private void setSolar(int yearInt, final int monthInt, int dayInt) {
        // 添加大小月月份并将其转换为list,方便之后的判断
        String[] months_big = {"1", "3", "5", "7", "8", "10", "12"};
        String[] months_little = {"4", "6", "9", "11"};

        final List<String> list_big = Arrays.asList(months_big);
        final List<String> list_little = Arrays.asList(months_little);

        currentYear = yearInt;
        // 年
        year.setAdapter(new NumericWheelAdapter(startYear, endYear));// 设置"年"的显示数据


        year.setCurrentItem(yearInt - 1900);// 初始化时显示的数据
//        year.setGravity(gravity);
        // 月
        month.setAdapter(new NumericWheelAdapter(startMonth, endMonth));
        month.setCurrentItem(monthInt - 1);
//        month.setGravity(gravity);
        // 日

        boolean leapYear = (yearInt % 4 == 0 && yearInt % 100 != 0) || yearInt % 400 == 0;
        if (startYear == endYear && startMonth == endMonth) {
            if (list_big.contains(String.valueOf(monthInt + 1))) {
                if (endDay > 31) {
                    endDay = 31;
                }
                day.setAdapter(new NumericWheelAdapter(startDay, endDay));
            } else if (list_little.contains(String.valueOf(monthInt + 1))) {
                if (endDay > 30) {
                    endDay = 30;
                }
                day.setAdapter(new NumericWheelAdapter(startDay, endDay));
            } else {
                // 闰年
                if (leapYear) {
                    if (endDay > 29) {
                        endDay = 29;
                    }
                    day.setAdapter(new NumericWheelAdapter(startDay, endDay));
                } else {
                    if (endDay > 28) {
                        endDay = 28;
                    }
                    day.setAdapter(new NumericWheelAdapter(startDay, endDay));
                }
            }
            day.setCurrentItem(dayInt - startDay);
        } else if (yearInt == startYear && monthInt + 1 == startMonth) {
            // 起始日期的天数控制
            if (list_big.contains(String.valueOf(monthInt + 1))) {

                day.setAdapter(new NumericWheelAdapter(startDay, 31));
            } else if (list_little.contains(String.valueOf(monthInt + 1))) {

                day.setAdapter(new NumericWheelAdapter(startDay, 30));
            } else {
                // 闰年 29，平年 28
                day.setAdapter(new NumericWheelAdapter(startDay, leapYear ? 29 : 28));
            }
            day.setCurrentItem(dayInt - startDay);
        } else if (yearInt == endYear && monthInt + 1 == endMonth) {
            // 终止日期的天数控制
            if (list_big.contains(String.valueOf(monthInt + 1))) {
                if (endDay > 31) {
                    endDay = 31;
                }
                day.setAdapter(new NumericWheelAdapter(1, endDay));
            } else if (list_little.contains(String.valueOf(monthInt + 1))) {
                if (endDay > 30) {
                    endDay = 30;
                }
                day.setAdapter(new NumericWheelAdapter(1, endDay));
            } else {
                // 闰年
                if (leapYear) {
                    if (endDay > 29) {
                        endDay = 29;
                    }
                    day.setAdapter(new NumericWheelAdapter(1, endDay));
                } else {
                    if (endDay > 28) {
                        endDay = 28;
                    }
                    day.setAdapter(new NumericWheelAdapter(1, endDay));
                }
            }
            day.setCurrentItem(dayInt - 1);
        } else {
            // 判断大小月及是否闰年,用来确定"日"的数据
            if (list_big.contains(String.valueOf(monthInt + 1))) {
                day.setAdapter(new NumericWheelAdapter(1, 31));
            } else if (list_little.contains(String.valueOf(monthInt + 1))) {
                day.setAdapter(new NumericWheelAdapter(1, 30));
            } else {
                // 闰年 29，平年 28
                day.setAdapter(new NumericWheelAdapter(startDay, leapYear ? 29 : 28));
            }
            day.setCurrentItem(dayInt - 1);
        }

//        day.setGravity(gravity);

        // 添加"年"监听
        year.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                int year_num = index + startYear;
                currentYear = year_num;
                int currentMonthItem = month.getCurrentItem();//记录上一次的item位置
                // 判断大小月及是否闰年,用来确定"日"的数据
                if (startYear == endYear) {
                    //重新设置月份
                    month.setAdapter(new NumericWheelAdapter(startMonth, endMonth));

                    if (currentMonthItem > month.getAdapter().getItemsCount() - 1) {
                        currentMonthItem = month.getAdapter().getItemsCount() - 1;
                        month.setCurrentItem(currentMonthItem);
                    }

                    int monthNum = currentMonthItem + startMonth;

                    if (startMonth == endMonth) {
                        //重新设置日
                        setReDay(year_num, monthNum, startDay, endDay, list_big, list_little);
                    } else if (monthNum == startMonth) {
                        //重新设置日
                        setReDay(year_num, monthNum, startDay, 31, list_big, list_little);
                    } else if (monthNum == endMonth) {
                        setReDay(year_num, monthNum, 1, endDay, list_big, list_little);
                    } else {//重新设置日
                        setReDay(year_num, monthNum, 1, 31, list_big, list_little);
                    }
                } else if (year_num == startYear) {//等于开始的年
                    //重新设置月份
                    month.setAdapter(new NumericWheelAdapter(startMonth, 12));

                    if (currentMonthItem > month.getAdapter().getItemsCount() - 1) {
                        currentMonthItem = month.getAdapter().getItemsCount() - 1;
                        month.setCurrentItem(currentMonthItem);
                    }

                    int month = currentMonthItem + startMonth;
                    if (month == startMonth) {
                        //重新设置日
                        setReDay(year_num, month, startDay, 31, list_big, list_little);
                    } else {
                        //重新设置日
                        setReDay(year_num, month, 1, 31, list_big, list_little);
                    }

                } else if (year_num == endYear) {
                    //重新设置月份
                    month.setAdapter(new NumericWheelAdapter(1, endMonth));
                    if (currentMonthItem > month.getAdapter().getItemsCount() - 1) {
                        currentMonthItem = month.getAdapter().getItemsCount() - 1;
                        month.setCurrentItem(currentMonthItem);
                    }
                    int monthNum = currentMonthItem + 1;

                    if (monthNum == endMonth) {
                        //重新设置日
                        setReDay(year_num, monthNum, 1, endDay, list_big, list_little);
                    } else {
                        //重新设置日
                        setReDay(year_num, monthNum, 1, 31, list_big, list_little);
                    }

                } else {
                    //重新设置月份
                    month.setAdapter(new NumericWheelAdapter(1, 12));
                    //重新设置日
                    setReDay(year_num, month.getCurrentItem() + 1, 1, 31, list_big, list_little);
                }

            }
        });


        // 添加"月"监听
        month.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                int month_num = index + 1;

                if (startYear == endYear) {
                    month_num = month_num + startMonth - 1;
                    if (startMonth == endMonth) {
                        //重新设置日
                        setReDay(currentYear, month_num, startDay, endDay, list_big, list_little);
                    } else if (startMonth == month_num) {

                        //重新设置日
                        setReDay(currentYear, month_num, startDay, 31, list_big, list_little);
                    } else if (endMonth == month_num) {
                        setReDay(currentYear, month_num, 1, endDay, list_big, list_little);
                    } else {
                        setReDay(currentYear, month_num, 1, 31, list_big, list_little);
                    }
                } else if (currentYear == startYear) {
                    month_num = month_num + startMonth - 1;
                    if (month_num == startMonth) {
                        //重新设置日
                        setReDay(currentYear, month_num, startDay, 31, list_big, list_little);
                    } else {
                        //重新设置日
                        setReDay(currentYear, month_num, 1, 31, list_big, list_little);
                    }

                } else if (currentYear == endYear) {
                    if (month_num == endMonth) {
                        //重新设置日
                        setReDay(currentYear, month.getCurrentItem() + 1, 1, endDay, list_big, list_little);
                    } else {
                        setReDay(currentYear, month.getCurrentItem() + 1, 1, 31, list_big, list_little);
                    }

                } else {
                    //重新设置日
                    setReDay(currentYear, month_num, 1, 31, list_big, list_little);
                }

            }
        });

    }


    private void setReDay(int year_num, int monthNum, int startD, int endD, List<String> list_big, List<String> list_little) {
        int currentItem = day.getCurrentItem();

//        int maxItem;
        if (list_big.contains(String.valueOf(monthNum))) {
            if (endD > 31) {
                endD = 31;
            }
            day.setAdapter(new NumericWheelAdapter(startD, endD));
//            maxItem = endD;
        } else if (list_little.contains(String.valueOf(monthNum))) {
            if (endD > 30) {
                endD = 30;
            }
            day.setAdapter(new NumericWheelAdapter(startD, endD));
//            maxItem = endD;
        } else {
            if ((year_num % 4 == 0 && year_num % 100 != 0)
                    || year_num % 400 == 0) {
                if (endD > 29) {
                    endD = 29;
                }
                day.setAdapter(new NumericWheelAdapter(startD, endD));
//                maxItem = endD;
            } else {
                if (endD > 28) {
                    endD = 28;
                }
                day.setAdapter(new NumericWheelAdapter(startD, endD));
//                maxItem = endD;
            }
        }

        if (currentItem > day.getAdapter().getItemsCount() - 1) {
            currentItem = day.getAdapter().getItemsCount() - 1;
            day.setCurrentItem(currentItem);
        }
    }
    */

}
